package com.kh.pajx.sop.view.action;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.kh.pajx.sop.base.ModelDrivenBaseAction;
import com.kh.pajx.sop.domain.SysArea;
import com.kh.pajx.sop.domain.SysDeptArea;
import com.kh.pajx.sop.util.common.JsonUtils;
import com.kh.pajx.sop.util.web.Constants;
import com.opensymphony.xwork2.ActionContext;

/**
 * 地区编码action
 * 
 * @author lipengfei 2014/6/26
 * 
 */
@Controller("sysAreaAction")
@Scope("prototype")
public class SysAreaAction extends ModelDrivenBaseAction<SysArea> {
	
	public String getAreaByDept(){
		Map<String,String> areaMap = new HashMap<String,String>();
		Map<String,String> appAreaMap = (Map<String,String>)ActionContext.getContext().getApplication().get("areaParentMap");
		if(model.getAreaCode().equals("0000")){
			areaMap = appAreaMap;
		}else{
			List<SysDeptArea> deptAreaList=sysDeptAreaService.getSysDeptAreaByDeptCode(model.getAreaCode());//查出原部门绑定的地区
			for (SysDeptArea deptArea : deptAreaList) {
				areaMap.put(deptArea.getAreaCode(), appAreaMap.get(deptArea.getAreaCode()));
			}
		}
		
		ActionContext.getContext().put("areaMap", areaMap);
		return "areaJson";
	}
	
	// ========================================

	private List<SysArea> sysAreaList;
	private String jsons = "";

	/**
	 * 获取地区编码
	 * 
	 * @return
	 * @author lipengfei 2014/6/26
	 * @throws IOException
	 */
	public void getSysAreaList() throws IOException {
		sysAreaList = sysAreaService.findAll();
		if (sysAreaList != null) {
			if (!sysAreaList.isEmpty()) {
				// 返回结果转换为json形式的字符串
				jsons = JsonUtils.toJSON(sysAreaList);
				HttpServletResponse response = ServletActionContext
						.getResponse();
				response.setCharacterEncoding(Constants.UTF8);
				response.getWriter().write(jsons);
			}
		}
	}
	// ===============================

	public void setSysAreaList(List<SysArea> sysAreaList) {
		this.sysAreaList = sysAreaList;
	}
}
